package com.sentinel.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.sentinel.client.UserClient;
import com.sentinel.common.Result;
import com.sentinel.sentinel.SentinelHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 谢阳
 * @version 1.0
 * @date 2023/11/21 16:24
 * @description
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserClient userClient;

    @SentinelResource(value = "degradation",
            fallbackClass = SentinelHandler.class, blockHandlerClass = SentinelHandler.class,
            fallback = "fallback", blockHandler = "blockHandler")
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable("id") Long id) throws InterruptedException {
        if (id == 101) {
            Thread.sleep(60);
        } else if (id == 102) {
            throw new RuntimeException("get user by id 102 error");
        }
        return userClient.getUserById(id);
    }

}


